package cn.iocoder.yudao.module.trade.scan.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.controller.DetailController;
import cn.iocoder.yudao.framework.business.basic.controller.PageController;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.trade.scan.service.AdminScanOrderService;
import cn.iocoder.yudao.module.trade.scan.vo.AdminScanOrderQueryDTO;
import cn.iocoder.yudao.module.trade.scan.vo.AdminScanOrderVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 线下扫码订单")
@RestController
@RequestMapping("/trade/order/offline-scan")
@Validated
public class ScanOrderController implements DetailController<AdminScanOrderVO, Long>, PageController<AdminScanOrderVO, AdminScanOrderQueryDTO> {
    @Resource
    private AdminScanOrderService adminScanOrderService;

    @Resource
    private ShopInfoApi shopInfoApi;
    @Resource
    private MemberUserApi memberUserApi;

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin','shop_admin')")
    @Override
    public CommonResult<AdminScanOrderVO> getDetail(Long id) {
        return success(adminScanOrderService.getDetail(id));
    }

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin','shop_admin')")
    @Override
    public CommonResult<PageResult<AdminScanOrderVO>> getPage(Integer pageNo, Integer pageSize, AdminScanOrderQueryDTO param) {
        PageResult<AdminScanOrderVO> page = adminScanOrderService.getPage(pageNo, pageSize, param);
        if (CollUtil.isEmpty(page.getList())) {
            return success(PageResult.empty());
        }
        Set<Long> shopIds = page.getList().stream().map(AdminScanOrderVO::getShopId).collect(Collectors.toSet());
        Map<Long, ShopInfoDTO> shops = shopInfoApi.getShopInfoMap(shopIds);
        Set<Long> userIds = page.getList().stream().map(AdminScanOrderVO::getUserId).collect(Collectors.toSet());
        Map<Long, UserSimpleVO> users = memberUserApi.getUserSimpleMap(userIds);
        page.getList().forEach(order -> {
            ShopInfoDTO shop = shops.getOrDefault(order.getShopId(), ShopInfoDTO.FALLBACK);
            order.setShop(shop);
            order.setUser(users.getOrDefault(order.getUserId(), UserSimpleVO.ANONYMOUS));
        });
        return success(page);
    }

}